<?php
session_start();
include_once 'mod.php';
$mod = new mod();

if (!isset($_SESSION['user'])){
    die('请先<a href="login.php">登录</a>'); ;
}else{
    $mysql = $mod->link();
    $sql = "select g.*,c.class_name from goods g inner join classification c on g.class = c.id";
    $result = $mod->select($mysql,$sql);

    if ($_SERVER['REQUEST_METHOD']=='POST'){
        $good_name = $_POST['good_name'];
        $class = $_POST['class'];
        $style = $_POST['style'];
        $create_time = $_POST['create_time'];
        $update_time = $_POST['update_time'];

        $where = 'where';
        if (!empty($good_name)){
            $sql.=" $where `good_name` like '%$good_name%'";
            $where = 'and';
        }
        if (!empty($class)){
            $sql.=" $where `class` = $class ";
            $where = 'and';
        }
        if (!empty($style)){
            $sql.=" $where `style` = $style ";
            $where = 'and';
        }
        if (!empty($create_time)){
            $sql.=" $where `create_time` >= '$create_time' ";
            $where = 'and';
        }
        if (!empty($update_time)){
            $sql.=" $where `update_time` <= '$update_time' ";
            $where = 'and';
        }
//        print_r($sql);
        $result = $mod->select($mysql,$sql);
    }
    $sql = "select * from classification";
    $result1 = $mod->select($mysql,$sql);
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>首页</title>
</head>
<body>
<a href="add.php">新增</a>
<form action="index.php" method="post">
    商品名:<input type="text" name="good_name" value="<?=$_POST['name']?>">
    类型:<select name="class">
        <option value="">===请选择类型===</option>
        <?php foreach ($result1 as $item){?>
        <option value="<?=$item['id']?>" <?= $_POST['class']==$item['id']?'selected': ''?>><?=$item['class_name']?></option>
        <?php }?>
    </select>
    状态:<select name="style">
        <option value="">===请选择类型===</option>
            <option value="1" <?= $style==1?'selected': ''?>>在售</option>
            <option value="2" <?= $style==2?'selected': ''?>>售罄</option>
    创造时间:<input type="date" name="create_time" value="<?=$create_time?>">
    ------
    更新时间:<input type="date" name="update_time" value="<?=$update_time?>">
    <input type="submit" value="搜索">
</form>
<table border="1" cellpadding="10px" cellspacing="0">
    <tr>
        <th>id</th>
        <th>商品名</th>
        <th>图像</th>
        <th>类型</th>
        <th>状态</th>
        <th>创造时间</th>
        <th>更新时间</th>
        <th>操作</th>
    </tr>
    <?php foreach ($result as $item){?>
        <tr>
            <td><?=$item['id']?></td>
            <td><?=$item['good_name']?></td>
            <td><?=$item['img']?></td>
            <td><?=$item['class_name']?></td>
            <td><?=$item['style']==1?'在售':($item['style']==2?'售罄':'异常')?></td>
            <td><?=$item['create_time']?></td>
            <td><?=$item['update_time']?></td>
            <td><a href="update.php?id=<?=$item['id']?>">修改</a>----<a href="delete.php?id=<?=$item['id']?>">删除</a></td>
        </tr>
    <? }?>
</table>
</body>
</html>
